package com.example.my_model.loginactivity;

import com.example.common.mvvm.repository.BaseRepository;
import com.example.my_model.ApiServer;
import com.example.my_model.accountentity.AccountEntity;
import com.example.my_model.accountentity.DataDTO;
import com.example.my_model.entity.UserInsertEntity;
import com.example.my_model.room.UserDao;
import com.example.my_model.room.UserEntity;
import com.example.network_skd.BaseEntity;
import com.example.network_skd.SingletonRetrofit;

import io.reactivex.Observable;
import io.reactivex.Observer;


public class LoginRepository extends BaseRepository {
    public Observable<BaseEntity> insertUser(UserDao userDao, DataDTO dataDTO){
        Observable<UserInsertEntity> observable = new Observable<UserInsertEntity>() {
            @Override
            protected void subscribeActual(Observer<? super UserInsertEntity> observer) {
                userDao.deleteUser(dataDTO);
                userDao.insertUser(dataDTO);
                UserInsertEntity userInsertEntity = new UserInsertEntity();
                userInsertEntity.status = 200;
                userInsertEntity.message = "保存成功";
                observer.onNext(userInsertEntity);
                observer.onComplete();
            }
        };

        return observableFunc(observable);
    }

    public Observable<BaseEntity> requestAddAccount(UserEntity userEntity){
        Observable<AccountEntity> accountEntityObservable = SingletonRetrofit.getInstance()
                .getRetrofit()
                .create(ApiServer.class)
                .requestInsertUser(userEntity.figureurl, "", userEntity.expires_time, 0, 0, userEntity.nickname, userEntity.qqOpenId, 0);


        return checkNetWork(accountEntityObservable);

    }
}
